package com.stylefeng.guns.modular.system.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.system.dao.ParamsMapper;
import com.stylefeng.guns.modular.system.model.Gpc;
import com.stylefeng.guns.modular.system.model.Params;
import com.stylefeng.guns.modular.system.service.IGpcService;
import com.stylefeng.guns.modular.system.warpper.GnGpcWarpper;
import com.stylefeng.guns.modular.system.warpper.GpcWarpper;

/**
 * 股票池控制器
 *
 * @author fengshuonan
 * @Date 2018-06-24 19:40:13
 */
@Controller
@RequestMapping("/gpc")
public class GpcController extends BaseController {

    private String PREFIX = "/system/gpc/";

    @Autowired
    private IGpcService gpcService;
    
    @Autowired
	private ParamsMapper paramsMapper;

    /**
     * 跳转到股票池首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "gpc.html";
    }
    
    /**
     * 跳转到股票池首页
     */
    @RequestMapping("/gn")
    public String gnindex() {
        return PREFIX + "gngpc.html";
    }

    /**
     * 跳转到添加股票池
     */
    @RequestMapping("/gpc_add")
    public String gpcAdd() {
        return PREFIX + "gpc_add.html";
    }
    
    /**
     * 跳转到添加股票池
     */
    @RequestMapping("/gpcCur_add")
    public String gpcCurAdd() {
        return PREFIX + "gpcCur_add.html";
    }

    /**
     * 跳转到修改股票池
     */
    @RequestMapping("/gpc_update/{gpcId}")
    public String gpcUpdate(@PathVariable Integer gpcId, Model model) {
        Gpc gpc = gpcService.selectById(gpcId);
        model.addAttribute("item",gpc);
        LogObjectHolder.me().set(gpc);
        return PREFIX + "gpc_edit.html";
    }

    /**
     * 获取股票池列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String date,@RequestParam(required = false) String dm) {
    	Page<Gpc> page = new PageFactory<Gpc>().defaultPage();
    	List<Map<String, Object>> gdhs = gpcService.selectMapsByDm(page,date,dm);
    	page.setRecords((List<Gpc>) new GpcWarpper(gdhs).warp());
        return super.packForBT(page);
    }
    
    /**
     * 获取股票池列表
     */
    @RequestMapping(value = "/gn/list")
    @ResponseBody
    public Object gnlist(@RequestParam(required = false) String gn,@RequestParam(required = false) String kpd) {
    	Page<Gpc> page = new PageFactory<Gpc>().defaultPage();
    	List<Map<String, Object>> gdhs = gpcService.selectMapsByGn(page,gn,kpd);
    	page.setRecords((List<Gpc>) new GnGpcWarpper(gdhs).warp());
        return super.packForBT(page);
    }
    
    /**
     * 新增股票池
     */
    @RequestMapping(value = "/update_fk")
    @ResponseBody
    public Object updatefk(String token,String userid) {
    	Params params = new Params();
		params.setgUserid(userid);
		params = paramsMapper.selectOne(params);
		if(params != null){
			params.setgToken(token);
			paramsMapper.updateById(params);
		}else{
			params = new Params();
			params.setgUserid(userid);
			params.setgToken(token);
			paramsMapper.insert(params);
		}
        return SUCCESS_TIP;
    }
    
    /**
     * 新增股票池
     */
    @RequestMapping(value = "/updateCur_fk")
    @ResponseBody
    public Object updateCurfk(String time,String token,String userid) {
        gpcService.updateCurfk(time,token,userid);
        return SUCCESS_TIP;
    }

    /**
     * 新增股票池
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Gpc gpc) {
        gpcService.insert(gpc);
        return SUCCESS_TIP;
    }

    /**
     * 删除股票池
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer gpcId) {
        gpcService.deleteById(gpcId);
        return SUCCESS_TIP;
    }

    /**
     * 修改股票池
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Gpc gpc) {
        gpcService.updateById(gpc);
        return SUCCESS_TIP;
    }

    /**
     * 股票池详情
     */
    @RequestMapping(value = "/detail/{gpcId}")
    @ResponseBody
    public Object detail(@PathVariable("gpcId") Integer gpcId) {
        return gpcService.selectById(gpcId);
    }
}
